#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "utils.h"
#include "child.h"

#define child_self(obj) cobject_self((obj), CCLASS_NAME(child_s))

static void inline __child_ctor(child_s *self)
{
    parent_ctor(&self->pobj);
    cobject_override(&self->pobj, parent_get_val, child_get_val);
    cobject_override(&self->pobj, parent_set_val, child_set_val);
    cobject_inherit(self, &self->pobj);
    self->c = 0;
}

static void inline __child_dtor(child_s *self)
{
    parent_dtor(&self->pobj);
    self->c = 0;
}

child_s *child_new(void)
{
    child_s *self = cobject_new(CCLASS_INFO(child_s));
    if (self) __child_ctor(self);
    return self;
}

void child_del(child_s **pself)
{
    assert(pself);
    assert(child_type(*pself));

    if (!(*pself)) return;

    __child_dtor(*pself);
    cobject_del(*pself);
    *pself= NULL;
}

int child_ctor(child_s *self)
{
    assert(self);

    cobject_ctor(self, CCLASS_INFO(child_s));
    __child_ctor(self);
    return 0;
}

void child_dtor(child_s *self)
{
    assert(self);
    assert(child_type(self));

    __child_dtor(self);
    cobject_dtor(self);
}

int child_print(cobject obj)
{
    child_s *self = obj;

    assert(obj);
    assert(child_type(obj));

    parent_print(&self->pobj);
    debug("c=%d\n", self->c);
    return 0;
}

int child_get_val(cobject obj)
{
    child_s *self = obj;

    assert(obj);
    assert(child_type(obj));

    return self->c;
}

int child_set_val(cobject obj, int val)
{
    child_s *self = obj;

    assert(obj);
    assert(child_type(obj));

    self->c = val;
    return 0;
}
